#include "DFA.h"

    // 添加状态
    void DFA::add_state(const char *name, bool isFinal, bool need_retract_, const char *type_) {
        states.emplace_back(name, isFinal, need_retract_, type_);
        if (initialState == nullptr) {
            initialState = &states.back();
            currentState = initialState;
        }
    }

    // 设置初始状态
    void DFA::set_initial(const char *name) {
        for (auto& state : states) {
            if (state.name == name) {
                initialState = &state;
                currentState = initialState;
                return;
            }
        }
        std::cerr << "Error: State " << name << " not found!" << std::endl;
    }

    // 设置最终状态
    void DFA::set_final(const char *name) {
        for (auto& state : states) {
            if (state.name == name) {
                state.isFinal = true;
                return;
            }
        }
        std::cerr << "Error: State " << name << " not found!" << std::endl;
    }

    // 添加转换规则
    void DFA::add_transition(const char *from, char input, const char *to) {
        DFAState* fromState = nullptr;
        DFAState* toState = nullptr;
        
        // 查找from和to状态
        for (auto& state : states) {
            if (state.name == from) fromState = &state;
            if (state.name == to) toState = &state;
        }
        
        if (fromState && toState) {
            alphabet.insert(input);
            transitionTable[{fromState, input}] = toState;
        } else {
            std::cerr << "Error: Invalid state names in transition!" << std::endl;
        }
    }

    void DFA::add_non_transition(const char *from, char input, const char *to){
        DFAState* fromState = nullptr;
        DFAState* toState = nullptr;
        
        // 查找from和to状态
        for (auto& state : states) {
            if (state.name == from) fromState = &state;
            if (state.name == to) toState = &state;
        }
        
        if (fromState && toState) {
            non_transitionTable[fromState].first.insert(input);
            non_transitionTable[fromState].second = toState;
        } else {
            std::cerr << "Error: Invalid state names in transition!" << std::endl;
        }
    }

    // 状态转换函数
    bool DFA::next(char input)
    {
        if (currentState == nullptr) {
            std::cerr << "Error: DFA not properly initialized!" << std::endl;
            return false;
        }
        
        auto key = std::make_pair(currentState, input);
        if (transitionTable.find(key) != transitionTable.end()) {
            currentState = transitionTable[key];
        } else if(non_transitionTable.find(currentState) != non_transitionTable.end()){
            if(non_transitionTable[currentState].first.find(input) == non_transitionTable[currentState].first.end()){
                currentState = non_transitionTable[currentState].second;
            }
            else{
                std::cerr << "Error: No transition defined for input '" << input << "' in state " << currentState->name << std::endl;
                return false;
            }
        } else{
            std::cerr << "Error: No transition defined for input '" << input << "' in state " << currentState->name << std::endl;
            return false;
        }
        return true;
    }

    // 重置到初始状态
    void DFA::reset() {
        currentState = initialState;
    }

    // 打印DFA信息
    void DFA::print() {
        std::cout << "DFA Information:" << std::endl;
        std::cout << "States: ";
        for (const auto& state : states) {
            std::cout << state.name << (state.isFinal ? "(F) " : " ");
        }
        std::cout << "\nAlphabet: ";
        for (char c : alphabet) {
            std::cout << c << " ";
        }
        std::cout << "\nInitial State: " << initialState->name << std::endl;
        std::cout << "Transition Table:" << std::endl;
        for (const auto& entry : transitionTable) {
            std::cout << entry.first.first->name << " --" << entry.first.second << "--> " << entry.second->name << std::endl;
        }
    }